/**
 * Created by zq on 2017/1/10.
 */
function initTree(dom,title,zNodes){
    var treeDom='<div class="ibox-title">'+
        '<h5>'+title+'</h5>'+
        '</div>'+
        '<div class="ibox-content clearfix">'+
        '<div class="ztreeBak left">'+
        '<ul id="'+dom+'" class="ztree"></ul>'+
        '</div>'+
        '</div>'
    $('.ztreeInit').append(treeDom);
    var setting = {
        view: {
            addHoverDom: addHoverDom,
            removeHoverDom: removeHoverDom,
            selectedMulti: false
        },
        check: {
            enable: true
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        edit: {
            enable: true,
            showRemoveBtn: setRemoveBtn,
            showRenameBtn: setRenameBtn
        },
        callback: {
            onClick: zTreeOnClick
        }
    };
    //初始化树
    $.fn.zTree.init($('#'+dom), setting, zNodes);
    //初始化高度
    var navHeight=window.parent.navHeight()+$('.title-height').outerHeight();
    var height=$(window).height()-navHeight;
    $('#'+dom).height(height);
}
//是否显示删除图标
function setRemoveBtn(treeId, treeNode) {
    return  !treeNode.isParent;
}
//是否显示编辑图标
function setRenameBtn(treeId, treeNode) {
    return !treeNode.isParent;
}
var newCount = 1;
//鼠标移动到节点上时，显示自定义图标或控件，
function addHoverDom(treeId, treeNode) {
    var sObj = $("#" + treeNode.tId + "_span");
    if (treeNode.editNameFlag || $("#addBtn_"+treeNode.tId).length>0) return;
    var addStr = "<span class='button add' id='addBtn_" + treeNode.tId
        + "' title='add node' onfocus='this.blur();'></span>";
    sObj.after(addStr);
    var btn = $("#addBtn_"+treeNode.tId);
    if (btn) btn.bind("click", function(){
        var zTree = $.fn.zTree.getZTreeObj("treeDemo");
        zTree.addNodes(treeNode, {id:(100 + newCount), pId:treeNode.id, name:"new node" + (newCount++)});
        return false;
    });
};
//鼠标移出节点时，隐藏图标或控件
function removeHoverDom(treeId, treeNode) {
    $("#addBtn_"+treeNode.tId).unbind().remove();
};
//点击节点时触发的回调函数
function zTreeOnClick(event, treeId, treeNode) {

};